package com.yunqiao.service.api.impl;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.Query;

import com.yunqiao.service.api.mapper.CarTypeMapper;
import com.yunqiao.service.api.entity.CarTypeEntity;
import com.yunqiao.service.api.CarTypeService;

import javax.annotation.Resource;


@Service("carTypeService")
public class CarTypeServiceImpl extends ServiceImpl<CarTypeMapper, CarTypeEntity> implements CarTypeService {

    @Resource
    private CarTypeMapper carTypeMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //各种查询条件的过滤,在此进行
        QueryWrapper<CarTypeEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(MapUtil.getStr(params, "name"))) {
            queryWrapper.like("name", "%" + MapUtil.getStr(params, "name") + "%");
        }
        queryWrapper.eq("version",0);
        IPage<CarTypeEntity> page = this.page(new Query<CarTypeEntity>(params).getPage(),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<Object> getCarType() {
        List<Object> list = new ArrayList<>();

        List<CarTypeEntity> carTypeEntities = carTypeMapper.selectList(new QueryWrapper<>());
        if (carTypeEntities != null) {
            Dict dict = Dict.create().set("label", "请选择").set("value", "");
            list.add(dict);
            for (CarTypeEntity carTypeEntity : carTypeEntities) {
                list.add(Dict.create().set("label", carTypeEntity.getName()).set("value", carTypeEntity.getId()));
            }
        }
        return list;
    }

}
